
#include "Driver_ek.h"
#include "Debug.h"
#include "i2c_module_interface.h"
#include "Driver_I2c.h"

Bool DriverEk_SetState(uint8 IicAddr, uint8 state)
{
	int nRet = 0;
	int counter = 0;
	uint8 buff[2]= {0};

	buff[0] = IicAddr;
	buff[1] = state;
	
	do
	{
		if(I2C_WORKSTA_IDLE == I2c_WorkState_Get(I2C_CHANNEL_LOCAL))
		{
			nRet = I2C_TxData(I2C_CHANNEL_LOCAL, 2, buff);
			if(LPC_SUCCESS != nRet )
			{
				goto Error;
			}
			else
			{
				break;
			}
		}
		
		if(++counter > 1000)
		{
			goto Error;
		}
		
	}while(1);

	Trace("%s 0x%x = 0x%x\n", _FUNC_, IicAddr, state);
	return True;
	
Error:
	Trace("%s(0x%x,0x%x) Failed.\n", _FUNC_, IicAddr, state);
	return False;
	
}

uint8 DriverEk_GetState(uint8 IicAddr)
{
	uint8 txBuff = 0;
	uint8 rxBuff[2] = {0};
	
	txBuff = IicAddr;
	if(2 != I2c_Device_Read (I2C_CHANNEL_LOCAL, &txBuff, 1, rxBuff,2))
	{
		Trace("%s(0x%x) Failed.\n", _FUNC_, IicAddr, rxBuff);
	}
	else

	{
		Trace("%s 0x%x = 0x%x\n", _FUNC_, IicAddr, rxBuff[1]);
	}

	return rxBuff[1];
}

void DriverEk_Init()
{
	/*
	DriverEk_GetState(PCF8574_1);
	DriverEk_GetState(PCF8574_2);
	DriverEk_GetState(PCF8574_3);
	DriverEk_GetState(PCF8574_4);

	DriverEk_SetState(PCF8574_1, 1);
	DriverEk_SetState(PCF8574_2, 2);
	DriverEk_SetState(PCF8574_3, 3);
	DriverEk_SetState(PCF8574_4, 0xFF);
	
	DriverEk_GetState(PCF8574_1);
	DriverEk_GetState(PCF8574_2);
	DriverEk_GetState(PCF8574_3);
	DriverEk_GetState(PCF8574_4);
	*/
}


